//
//  Direction.h
//  Dungeons and Princesses
//
//  Created by John Grange on 10/17/11.
 //*Updated Nov 5, 2011 - River Warren
//  Copyright (c) 2011 SD Networks. All rights reserved.
//

#ifndef Dungeons_and_Princesses_Direction_h
#define Dungeons_and_Princesses_Direction_h

#include <ostream>
#include <istream>
#include <string.h>
#include <stdlib.h>
#include "Base.h"
#include "Door.h"
#include "Utility.h"

using std::string;
using std::ostream;
using std::istream;

class Direction: public Base
{

private:
    string directionName;   //this should contain North, South, East, West
    string nextRoomID;      //this should contain the roomID of the next room
    Door doorObject;
    bool hasDoor;

public:
    Direction(string direction): Base("Direction") {

        hasDoor = false;
        directionName = Utility::removeGarbage(direction);
        nextRoomID = "";
    }

    Direction(): Base("Direction")
    {
        hasDoor = false;
        nextRoomID = "";

    }

    void setDesc(std::string desc) {

        Base::setDesc(desc);

    }

    void setNextRoomID(std::string nextRoom) {

        nextRoomID = Utility::removeGarbage(nextRoom);

    }

    std::string getRoomID() {
        if(hasDoor && doorObject.isLocked())
        {
            return "Locked";
        }
        return nextRoomID;

    }

    void setDoor(Door doorForDirection) {

        if (!hasDoor) {
            std::cout << "Error: This direction does not have a door!" << std::endl;
        }
        else {
            doorObject = doorForDirection;
            hasDoor = true;
        }
    }

    void setDirectionName(std::string direction) {
        directionName = Utility::removeGarbage(direction);

    }

    Door* getDoor() {
        return &doorObject;
    }

    bool getHasDoor() {
        return hasDoor;
    }

    ~Direction(){};


protected:
    void setHasDoor(int _hasDoor)
    {
        hasDoor = _hasDoor;
    }

    friend istream& operator>>(istream& in, Direction& d)
    {
        char *tk = NULL;// pointers to chars
        char data[MAX] = ""; //to hold line
        in.getline(data,MAX - 1, '\n');
        tk = strtok(data, DELIMS);  //get first token of new line
        d.setDesc(tk);
        tk = strtok(NULL, DELIMS); // get next token
        d.setHasDoor(atoi(tk));
        tk = strtok(NULL, DELIMS); // get next token
        d.setNextRoomID(tk);
        if(d.getHasDoor())
        {
            Door door;
            in >> door;
            d.setDoor(door);
        }
        return in;

    }

    friend ostream& operator<<(ostream& out, Direction& d)
    {
        out << d.getDesc() << ',' << d.getHasDoor() << ',' << d.nextRoomID;
        if(d.getHasDoor())
            out << std::endl << d.doorObject;
        return out;
    }
};

#endif


